#!/bin/sh
set -e

APP=DndTool


if [[ -z "${QTDIR}" ]]; then
    echo "Please ensure the \$QTDIR env var is set, eg"
    echo "export QTDIR=/usr/local/Trolltech/Qt-4.4.3"
    exit 1;
fi

if [[ -z "$1" ]]; then
    echo "Usage: build_linux_bin.sh 1.2.3"
    echo "(where 1.2.3 is the version number)"
    exit 1;
fi

if [[ ! -f ${APP}.pro ]]; then
    echo "Run this script from the root of the ${APP} source tree, eg:"
    echo "cd ${APP}"
    echo "./build_linux_bin.sh 1.2.3"
    exit 1;
fi

VER=$1

APP_TAR_GZ="../${APP}-${VER}.tar.gz"

RELEASE_DIR="../${APP}-${VER}"

mkdir -p ${RELEASE_DIR}

echo "Attempting to build ${APP}..."
set +e
make clean
set -e
${QTDIR}/bin/qmake -config release
make

rm -Rf ${RELEASE_DIR}
mkdir ${RELEASE_DIR}
cp -v DndTool ${RELEASE_DIR}/DndTool.bin
cp -P -r -v lib ${RELEASE_DIR}/.
cp -v README.html ${RELEASE_DIR}
cp -v INSTALL ${RELEASE_DIR}
cp -v COPYING ${RELEASE_DIR}
cp -v run.sh ${RELEASE_DIR}/DndTool
cp -P -v ${QTDIR}/lib/libQtXml.so* ${RELEASE_DIR}/lib/.
cp -P -v ${QTDIR}/lib/libQtCore.so* ${RELEASE_DIR}/lib/.
cp -P -v ${QTDIR}/lib/libQtGui.so* ${RELEASE_DIR}/lib/.

tar czvf ${APP_TAR_GZ} ${RELEASE_DIR}

echo "${APP_TAR_GZ} ready for distribution"
